package com.runa.rccprotocol.dto.param;

import com.runa.protocol.util.ByteUtil;

import java.util.Arrays;

public abstract class Param {

    private int index;

    void dataFormat6(float f, byte[] src, int offset) {
        if (f < 80) {
            int number = (int) (f * 100);
            src[offset] = (byte) ((number%10 & 0x0f) + (number/10%10 << 4));
            src[offset+1] = (byte) ((number/100%10 & 0x7f) + (number/1000%10 << 4));
            if (f < 0) {
                src[offset+1] = (byte) (src[offset+1] | 0x80);
            }
        }
    }

    void dataFormat7(float f, byte[] src, int offset) {
        if (f >= 1000) return;
        int number = (int) (f*10);
        src[offset] = (byte) ((number%10 & 0x0f) + (number/10%10 << 4));
        src[offset+1] = (byte) ((number/100%10 & 0x0f) + (number/1000%10 << 4));
    }

    void dataFormat11(float f, byte[] src, int offset) {
        if (f >= 1000000) return;
        int number = (int) (f*100);
        src[offset] = (byte) ((number%10 & 0x0f) + (number/10%10 << 4));
        src[offset+1] = (byte) ((number/100%10 & 0x0f) + (number/1000%10 << 4));
        src[offset+2] = (byte) ((number/10000%10 & 0x0f) + (number/100000%10 << 4));
        src[offset+3] = (byte) ((number/1000000%10 & 0x0f) + (number/10000000%10 << 4));
    }

    void dataFormat12(String str, byte[] src, int offset) {
        if (str.length() > 12) {
            str = str.substring(str.length() - 12);
        }
        while (str.length() < 12) {
            str = "0"+str;
        }
        byte[] bytes = ByteUtil.hexStringToByteFlip(str);
        for (byte b : bytes) {
            src[offset++] = b;
        }
    }

    /*void dataFormat12(String str, byte[] src, int offset) {
        char[] chars = str.toCharArray();
        if (chars.length%2 != 0) {
            char[] bs = newComm char[chars.length+1];
            System.arraycopy(chars, 0, bs, 1, bs.length - 1);
            chars = bs;
        }
        for (int i=chars.length-1, j=5; j>0 && i>0;) {
            src[offset + 5 - j] = (byte) ((chars[i] - 0x30) + (chars[i-1] - 0x30 << 4));
            i -= 2;
            j --;
        }
    }*/

    public static void main(String[] args) {
        /*IndexRecord record = newComm IndexRecord();
        record.setAddress("1234567");
        record.setIndex(1);
        record.setPort(4);
        record.setSign(1);
        record.setCode(40);
        record.setArea((float) 12.4);
        record.setType(0);
        record.setDoorplate("02010101");
        long s = System.nanoTime();
        byte[] bytes = record.getBytes();
        System.out.println(System.nanoTime()-s);
        System.out.println(ByteUtil.byteToHexString(bytes));*/
//        byte[] bs = newComm byte[2];
//        dataFormat6((float) 25.9, bs, 0);
//        System.out.println(ByteUtil.byteToHexString(bs));

        String s = "076c";
        byte[] bytes = new byte[4];
        IndexRecord record = new IndexRecord();
        record.dataFormat12(s, bytes, 0);
        System.out.println(ByteUtil.byteToHexString(bytes));
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }
}
